const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        user: [],
        grade_array: [],
        grade_index: 0,
        class_array: [],
        class_index: 0,
        grade_id: '',
        class_id: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            user: app.globalData.user
        })
        this.getGrade();
        this.getClass();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
       
    },

    // 提交
    doSubmit: function(e) {
        let data = e.detail.value;
        app._post('/api/login/user/updateUser', {
            realname: data.realname,
            student_no: data.student_no?data.student_no:'',
            teacher_no: data.teacher_no?data.teacher_no:'',
            grade_id: this.data.grade_id?this.data.grade_id:this.data.user.grade_id,
            class_id: this.data.class_id?this.data.class_id:this.data.user.class_id
        }, function(res) {
            wx.navigateBack({
                delta: 1
            })
        })
    },

    /**
     * 获取所有年级
     */
    getGrade: function () {
        let _this = this;
        app._get('/api/school/getGrade', null , function (response) {
            let data = response.data.data;
            let setdata = {};
            setdata['grade'] = data;
            _this.setData(setdata);
            if(_this.data.grade.length!=0){
                for (let i = 0; i <  _this.data.grade.length; i++) {
                    if(_this.data.user.grade_id == _this.data.grade[i]['id']){
                        _this.setData({
                            grade_index: i
                        })
                    }
                    _this.setData({
                        grade_array: _this.data.grade_array.concat(_this.data.grade[i]['name'])
                    })
                }
            }
        })
    },

    /**
     * 选择年级
     */
    bindPickerGrade: function (e) {
        let _this = this;
        _this.setData({
            grade_index: e.detail.value
        })
        for (let i = 0; i <  _this.data.grade.length; i++) {
            if(_this.data.grade_array[e.detail.value] == _this.data.grade[i]['name']){
                _this.setData({
                    grade_id: _this.data.grade[i]['id']
                })
            }
        }
        console.log(_this.data.grade_id)
    },

    /**
     * 获取所有班级
     */
    getClass: function () {
        let _this = this;
        app._get('/api/school/getClass', null , function (response) {
            let data = response.data.data;
            let setdata = {};
            setdata['class'] = data;
            _this.setData(setdata);
            if(_this.data.class.length!=0){
                for (let i = 0; i <  _this.data.class.length; i++) {
                    if(_this.data.user.class_id == _this.data.class[i]['id']){
                        _this.setData({
                            class_index: i
                        })
                    }
                    _this.setData({
                        class_array: _this.data.class_array.concat(_this.data.class[i]['name'])
                    })
                }
            }
        })
    },

    /**
     * 选择班级
     */
    bindPickerClass: function (e) {
        let _this = this;
        _this.setData({
            class_index: e.detail.value
        })
        for (let i = 0; i <  _this.data.class.length; i++) {
            if(_this.data.class_array[e.detail.value] == _this.data.class[i]['name']){
                _this.setData({
                    class_id: _this.data.class[i]['id']
                })
            }
        }
        console.log(_this.data.class_id)
    },
})